package com.iwop.moto.utils.chat;

import com.iwop.moto.utils.chat.api.ChatGroupAPI;
import com.iwop.moto.utils.chat.api.ChatRoomAPI;
import com.iwop.moto.utils.chat.api.IMUserAPI;
import com.iwop.moto.utils.chat.comm.ClientContext;
import com.iwop.moto.utils.chat.comm.EasemobRestAPIFactory;
import com.iwop.moto.utils.chat.comm.body.IMUserBody;
import com.iwop.moto.utils.chat.comm.wrapper.BodyWrapper;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created by Administrator on 2016/7/19.
 */
public class  ChatUtil {

    /**
     * 环信用户管理
     * */
    public static Object getIMUserInfo(String uid,String methodName){
        EasemobRestAPIFactory factory = ClientContext.getInstance().init(ClientContext.INIT_FROM_PROPERTIES).getAPIFactory();
        IMUserAPI user = (IMUserAPI)factory.newInstance(EasemobRestAPIFactory.USER_CLASS);
        Object invoke=null;
        try {
            Class<? extends IMUserAPI> aClass = user.getClass();
            Method method = aClass.getMethod(methodName, String.class);
            invoke = method.invoke(user, uid);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return invoke;
    }

    /**
     * 环信群组管理
     * */
    public static Object UserGroupManager(String methodName,Object[] arr){
        EasemobRestAPIFactory factory = ClientContext.getInstance().init(ClientContext.INIT_FROM_PROPERTIES).getAPIFactory();
        ChatGroupAPI chatgroup = (ChatGroupAPI)factory.newInstance(EasemobRestAPIFactory.CHATGROUP_CLASS);
        Object invoke=null;
        try {
            Class[] clz=new Class[arr.length];
            for (int i=0;i<arr.length;i++){
                String name = arr[i].getClass().getName();
                if(name.contains("ChatGroupBody")){
                    clz[i]=BodyWrapper.class;
                }else {
                    clz[i]=arr[i].getClass();
                }
            }
            Class<? extends ChatGroupAPI> aClass = chatgroup.getClass();
            Method method = aClass.getMethod(methodName,clz);
            invoke = method.invoke(chatgroup, arr);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return invoke;
    }


    /**
     * 聊天室管理
     * */
    public static Object userChatRoomManager(String methodName,Object[] arr){
        EasemobRestAPIFactory factory = ClientContext.getInstance().init(ClientContext.INIT_FROM_PROPERTIES).getAPIFactory();
        ChatRoomAPI chatroom = (ChatRoomAPI)factory.newInstance(EasemobRestAPIFactory.CHATROOM_CLASS);
        Object invoke=null;
        try {
            Class[] clz=new Class[arr.length];
            for (int i=0;i<arr.length;i++){
                String name = arr[i].getClass().getName();
                if(name.contains("ChatRoomBody")){
                    clz[i]=BodyWrapper.class;
                }else {
                    clz[i]=arr[i].getClass();
                }
            }
            Class<? extends ChatRoomAPI> aClass = chatroom.getClass();
             Method method = aClass.getMethod(methodName, clz);
            invoke = method.invoke(chatroom, arr);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return invoke;
    }

    /**
     * 添加环信用户
     * */
    public static Object addChatUser(String userId,String password,String nickName){
        EasemobRestAPIFactory factory = ClientContext.getInstance().init(ClientContext.INIT_FROM_PROPERTIES).getAPIFactory();
        IMUserAPI user = (IMUserAPI)factory.newInstance(EasemobRestAPIFactory.USER_CLASS);
        BodyWrapper userBody = new IMUserBody(userId, password, nickName);
        return user.createNewIMUserSingle(userBody);
    }

    /**
     * 修改昵称
     * */
    public static void updateChatUser(String uid,String nickName){
        EasemobRestAPIFactory factory = ClientContext.getInstance().init(ClientContext.INIT_FROM_PROPERTIES).getAPIFactory();
        IMUserAPI user = (IMUserAPI)factory.newInstance(EasemobRestAPIFactory.USER_CLASS);
        BodyWrapper userBody = new IMUserBody(nickName,null,null);
        user.modifyIMUserNickNameWithAdminToken(uid, userBody);
    }

    /**
     * 修改密码
     * */
    public static void updateChatUserPassword(String uid,String password){
        EasemobRestAPIFactory factory = ClientContext.getInstance().init(ClientContext.INIT_FROM_PROPERTIES).getAPIFactory();
        IMUserAPI user = (IMUserAPI)factory.newInstance(EasemobRestAPIFactory.USER_CLASS);
        BodyWrapper userBody = new IMUserBody(null,password,null);
        user.modifyIMUserPasswordWithAdminToken(uid, userBody);
    }
    /*
    public static Object updateGroup(Object o,String groupId){
        EasemobRestAPIFactory factory = ClientContext.getInstance().init(ClientContext.INIT_FROM_PROPERTIES).getAPIFactory();
        ChatGroupAPI chatgroup = (ChatGroupAPI)factory.newInstance(EasemobRestAPIFactory.CHATGROUP_CLASS);
        Object o1 = chatgroup.modifyChatGroup(groupId, o);
        return o1;
    }*/
}
